package com.blockmeta.bbs.overallserviceapplication.search.fragment;

import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blockmeta.bbs.baselibrary.utils.gson.GsonUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.base.fragment.BaseLazyBusinessListFragment;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.constant.NetConstant;
import com.blockmeta.bbs.overallserviceapplication.R;
import com.blockmeta.bbs.overallserviceapplication.pojo.CollegeListPOJO;
import com.blockmeta.bbs.overallserviceapplication.search.adapter.CollegeListAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

/**
 * Created by author on 2018/5/8.
 */
@Route(path = RouterPath.COLLEGE_LIST_FRAGMENT)
public class CollegeListFragment extends BaseLazyBusinessListFragment<List<CollegeListPOJO>> {
    private int pageNo = 1;

    @Autowired(name = Constant.ZERO)
    public String mKey;
    @Autowired(name = Constant.ONE)
    public String mValue;

    public static CollegeListFragment newInstance(String key, String value) {
        CollegeListFragment fragment = new CollegeListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Constant.ZERO, key);
        bundle.putString(Constant.ONE, value);
        fragment.setArguments(bundle);
        return fragment;

    }

    @Override
    public BaseQuickAdapter initAdapter() {
        return new CollegeListAdapter(R.layout.item_news_type2, null, mContext);
    }

    @Override
    public String getUrl() {
        return NetConstant.BBS_COLLEGE;
    }

    @Override
    public HashMap<String, String> initHashMap() {
        final HashMap<String, String> map = new HashMap<>();
        map.put("page", getPageNo());
        map.put(getArguments().getString(Constant.ZERO), getArguments().getString(Constant.ONE));
        return map;
    }

    @Override
    public List<CollegeListPOJO> parseData(String resp) {
      List<CollegeListPOJO> listPOJOS ;
        Type type = new TypeToken<List<CollegeListPOJO>>() {
        }.getType();
        try {
            listPOJOS = GsonUtil.buildGson().fromJson(resp, type);
        } catch (Exception e) {
            listPOJOS = null;
        }
        return listPOJOS;
    }

    @Override
    protected void adapterSetData() {
        mAdapter.setNewData(mPOJO);
    }

    @Override
    protected void adapterAddData() {
        if (mPOJO == null) {
            list_swipe.finishLoadmore();
            return;
        }
        mAdapter.addData(mPOJO);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        if (mPOJO != null  ) {
            pageNo++;
            setPageNo(String.valueOf(pageNo));
            initNet(true);
        } else {
            list_swipe.finishLoadmore();
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        setPageNo("1");
        pageNo = 1;
        initNet(false);
    }
}
